/**
 * FileName: ThreadPoolExecutorTest
 * Author:   powersi
 * Date:     2021/3/15 21:23
 * Description:
 */
package com.zm.demo.core.concurrent;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.logging.Logger;

/**
 * Description: 
 * Author:      powersi
 * Date:        2021/3/15 21:23
 */
public class ThreadPoolExecutorTest {

    private final static Logger LOGGER = Logger.getLogger(ThreadPoolExecutorTest.class.getSimpleName());
    public static void main(String[] args) {

        new ThreadPoolExecutorTest().upload();

    }

    public void upload(){
        ExecutorService executorService = new ThreadPoolExecutor(
                8,
                8,
                30L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100),
                new ThreadFactoryNct("DeductDetailUploadHandler"));

        List<Callable<Boolean>> callables = new ArrayList<>();

        for (int i = 0; i<10; i++) {
            final int ii = i;
            callables.add(() ->this.doUpload(ii));
        }

        try {
//            executorService.invokeAll(callables);

            callables.stream().map(c ->{
                try {
                    return c.call() ? 1 : 0;
                } catch (Exception e){
                    LOGGER.fine(e.getMessage());
                    return 0;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        executorService.shutdown();
    }

    public boolean doUpload(int i){
        LOGGER.info(Thread.currentThread().getName() + ": " + i);
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return true;
    }
}
